//
//  HomeViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/3.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "HomeViewController.h"
#import "HistoryViewController.h"
#import "PersonCenterViewController.h"
#import "CBaseTable.h"
#import "HomeModel.h"
#import "LoginViewController.h"
#import "ErrorHistoryViewController.h"
#import "ScanViewController.h"
#import "DetailsViewController.h"
#import "HomeView.h"
#import "HomeModel.h"
#import "SVPullToRefresh.h"
#import "CInputBoxView.h"
#import "SuspensionView.h"
#import "HomeMoreTable.h"
#import "ErrorHistoryView.h"
#import "ErrorHistoryModel.h"
#import "MessageViewController.h"
#import "MWPhotoBrowser.h"
#import "VoiceConverter.h"
#import "InputViewController.h"
#import "MyInputViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "PollingDetailViewController.h"
#import "EquipmentViewController.h"
#import "BankView.h"
#import "LoadingView.h"
#import "TimeView.h"
#import "InitiatePollingViewController.h"
#import "CompleteViewController.h"
#import "LocationErrorViewController.h"
@interface HomeViewController()<UITableViewDelegate,MWPhotoBrowserDelegate>
@property(nonatomic,strong)NSMutableArray *photosAr;
@property(nonatomic,strong)NSMutableArray *historyPhotosAr;
@property(nonatomic,strong)MWPhotoBrowser *homebrowser;
@property(nonatomic,strong)MWPhotoBrowser *historybrowser;
@property (nonatomic,strong)UIScrollView *passscrollview;
@property (nonatomic,strong)UIScrollView *noPassscrollview;
@property (nonatomic,assign) NSInteger moreNum;
@property (nonatomic,assign) NSInteger addNum;
@property (nonatomic, strong)ErrorHistoryView *errorHistory;
@property (nonatomic, strong) HomeMoreTable *homeTable;
@property (strong,nonatomic) HomeView *homeView;
@property (nonatomic, strong)UIImageView *headView;
@property (nonatomic, strong) UILabel *messageLab;
@property (nonatomic, strong) UIView *noDataView;
@property (nonatomic,strong) UIView *addView;
@property (nonatomic, strong) AFNetworkReachabilityManager *manager;
@property (nonatomic) BOOL isNetwork;


@end
@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated
{

    [self layoutContext];
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [_homeTable removeFromSuperview];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [_homeView refresh];
    [[[UIApplication sharedApplication] keyWindow] addSubview:_homeTable];
    [self hiddenMoreTable];
    
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return  UIStatusBarStyleLightContent;
}
- (void)initView{
    self.navBar.hidden=YES;
    self.hide=NO;
    [self noPass];
    [self pass];
    self.navBar.bgColor =[UIColor clearColor];
}

- (void)layoutContext
{
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMessage:) name:@"Operation_EnterForeground" object:nil];
    //审核通过
    if ([[CClass getCacheData:NSUD_state]isEqualToString:@"0"]) {
        
        _passscrollview.hidden=NO;
        _noPassscrollview.hidden=YES;
     
        [_homeView refresh];
    }else
    {
    
        _passscrollview.hidden=YES;
        _noPassscrollview.hidden=NO;
    }
    [self.view bringSubviewToFront: self.mainTabBar ];


  
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)getMessage:(NSNotification*)noti
{
    [_homeView refresh];

}
- (void)noPass
{
  
    _noPassscrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight-48)];
    _noPassscrollview.backgroundColor =BackColor;
    [self.view addSubview:_noPassscrollview];
    _noPassscrollview.tag=200;
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor=MainColor;
    [_noPassscrollview addSubview:narView];
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, WindowWidth, 26)];
    [narView addSubview:titleView];
    
    
    UILabel *lab =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 21)];
    [titleView addSubview:lab];
    lab.userInteractionEnabled=YES;
    lab.text =@"邮易维";
    lab.textAlignment=NSTextAlignmentCenter;
    lab.textColor =[UIColor whiteColor];
    lab.font = F(16);
    
    
    UITableView *tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64)];
    tableView.backgroundColor =[UIColor clearColor];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    tableView.tableHeaderView=[self HeaderView];
    tableView.showsPullToRefresh=YES;
    tableView.showsVerticalScrollIndicator=NO;
    self.navigationController.navigationBar.translucent = NO;
    
    __block typeof(tableView) blockself = tableView;
    [tableView addPullToRefreshWithActionHandler:^{
        
        
        NSString *uuid =[CClass getCacheData:NSUD_UUID];
        if ([uuid isEqualToString:@""]) {
            
        }else
        {
            [CClass HttpRequestTask:YES withUrl:@"reviseaccount_selectCustomer" withData:@{@"uuid":[CClass getCacheData:NSUD_UUID]} notifyError:YES onSuccess:^(BaseModel *model) {
                
                [CClass saveCacheData:[model.B_data objectForKey:@"state"] key:NSUD_state];
                //0已审核; 1待审核
                [self layoutContext];
                // 完成时调用
                [blockself.pullToRefreshView stopAnimating];
                blockself.pullToRefreshView.textColor =[UIColor grayColor];
                blockself.pullToRefreshView.arrowColor =[UIColor grayColor];
            } onError:^(BaseModel *model) {
                [blockself.pullToRefreshView stopAnimating];
                
            }];
        }
        
    }];
    [_noPassscrollview addSubview:tableView];

}
- (void)pass
{
//    self.navBar.hidden=YES;
//    self.hide=YES;
    self.isNetwork=YES;
    __block typeof (self)blockSelf=self;

    self.manager = [AFNetworkReachabilityManager manager];
    [self.manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status==AFNetworkReachabilityStatusNotReachable){
            
            blockSelf.isNetwork=NO;
        }else
        {
            blockSelf.isNetwork=YES;
            
        }
    }];
    [self.manager startMonitoring];
    _passscrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight-48)];
    _passscrollview.backgroundColor =[UIColor clearColor];
    [self.view addSubview:_passscrollview];
    _passscrollview.tag=100;
    __block HomeViewController *weakview = self;

    //历史报修
    _errorHistory =[[ErrorHistoryView alloc]initWithFrame:CGRectMake(-500, 64, WindowWidth, WindowHeight-64-49)];
    [self.view addSubview:_errorHistory];
    
    
    
#pragma mark 进入报修单详情页历史
    [_errorHistory setGoToNextDetail:^(ErrorHistoryModel* model) {
        [blockSelf hiddenMoreTable];
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            
            DetailsViewController *detailVc =[[DetailsViewController alloc]init];
            detailVc.warningUuid=model.E_uuid;
            detailVc.type=2;
            detailVc.state=model.E_state;
            [blockSelf pushController:detailVc];
        }
    }];
#pragma mark 进入巡检单详情页历史
    [_errorHistory setGoToHistoryContextPolling:^(ErrorHistoryModel* model) {
        [blockSelf hiddenMoreTable];
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            
            PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
            detailVc.warningUuid=model.E_uuid;
            detailVc.type=2;
            detailVc.state=model.E_state;
            [blockSelf pushController:detailVc];
        }
    }];
#pragma mark 播放语音首页
    [_homeView setPlaySound:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];
        
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
            
        }else
        {
        }
        
    }];
    
#pragma mark 放大图片历史

    
    [_errorHistory setBlowUpImg:^(ErrorHistoryModel* model,NSString* num) {
        [blockSelf hiddenMoreTable];

        _historyPhotosAr=[[NSMutableArray alloc]init];

        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            NSString*urlString =[CClass getCacheData:qnDomain];
            [blockSelf.historyPhotosAr removeAllObjects];
            NSString*imgStr=model.E_image;
            NSString *videoStr=model.E_video;
            
            if ((imgStr ==nil||[imgStr isEqualToString:@""])) {
                if (videoStr.length>0) {
#pragma mark 播放视频历史
                    
                    [blockSelf.historyPhotosAr addObject:[MWPhoto videoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,videoStr]]]];
                }
            }else
            {
                NSArray *array = [imgStr componentsSeparatedByString:@","];
                for (int i=0; i<array.count; i++) {
                    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,[array objectAtIndex:i]]];
                    [blockSelf.historyPhotosAr addObject:[MWPhoto photoWithURL:url]];
                    
                }
                if (videoStr.length>0) {
#pragma mark 播放视频历史
                    
                    [blockSelf.historyPhotosAr addObject:[MWPhoto videoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,videoStr]]]];
                }
            }
            
            
           _historybrowser = [[MWPhotoBrowser alloc] initWithDelegate:blockSelf];
            // Present
            [blockSelf.navigationController pushViewController:blockSelf.historybrowser animated:YES];
            blockSelf.historybrowser.displayActionButton = NO;
            blockSelf.historybrowser.displayNavArrows = NO;
            blockSelf.historybrowser.displaySelectionButtons = NO;
            blockSelf.historybrowser.zoomPhotosToFill = YES;
            blockSelf.historybrowser.alwaysShowControls = NO;
            blockSelf.historybrowser.enableGrid = YES;
            blockSelf.historybrowser.startOnGrid = NO;
            blockSelf.historybrowser.autoPlayOnAppear = NO;
            
            [blockSelf.historybrowser setCurrentPhotoIndex:[num integerValue]];
            
            UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
            [backView bk_whenTapped:^{
                [blockSelf.navigationController popViewControllerAnimated:YES];
            }];
            
            UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_backWhite"];
            UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, (40-icoBack.size.height)/2, icoBack.size.width, icoBack.size.height)];
            imgBack.image=icoBack;
            imgBack.userInteractionEnabled=YES;
            [backView addSubview:imgBack];
            
            UILabel *backTitle = [[UILabel alloc] init];
            backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 0, 50 - imgBack.frame.size.width,40);
            
            [backTitle setText:@"返回"];
            [backTitle setTextColor:[UIColor whiteColor]];
            [backTitle setFont:F(14)];
            [backView addSubview:backTitle];
            UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
            blockSelf.historybrowser.navigationItem.leftBarButtonItem = leftBarButtonItem;
            
        }
        
        
    }];

    
    
    
    
    _homeView =[[HomeView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64-49)];
    _homeView.hidden=NO;


#pragma mark 进入报修详情页
    [_homeView setGoToNextDetail:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];

        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            DetailsViewController *detailVc =[[DetailsViewController alloc]init];
            detailVc.warningUuid=model.H_uuid;
            detailVc.type=2;
            detailVc.state=model.H_state;
            [blockSelf pushController:detailVc];
        }
    }];
#pragma mark 进入报修接单

    [_homeView setCustomAccept:^(NSDictionary* dataDic) {
        [blockSelf hiddenMoreTable];
        
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {

            
           [TimeView showUuid:[dataDic objectForKey:@"orderUuid"] longitude:[dataDic objectForKey:@"longitude"] latitude:[dataDic objectForKey:@"latitude"] action:^(NSInteger index) {
               [blockSelf.homeView refresh];
           }];
            
           
            

        }
    }];



#pragma mark 进入巡检详情页

    [_homeView setGoToNextContextPolling:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];
        
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
            detailVc.warningUuid=model.H_uuid;
            detailVc.type=2;
            detailVc.state=model.H_state;
            [blockSelf pushController:detailVc];
        }
    }];
#pragma mark 进入巡检开始巡检按钮的详情页

    [_homeView setGoToNextStartContextPolling:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];
        
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
            detailVc.warningUuid=model.H_uuid;
            detailVc.type=2;
            detailVc.state=@"10";//处理中的状态
            [blockSelf pushController:detailVc];
        }
    }];
    
#pragma mark 进入进度页
    [_homeView setGoToNextProgress:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];

        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            
            DetailsViewController *detailVc =[[DetailsViewController alloc]init];
            detailVc.warningUuid=model.H_uuid;
            detailVc.type=1;
            detailVc.state=model.H_state;
            [blockSelf pushController:detailVc];
        }
    }];
#pragma mark 进入巡检进度页

    
    [_homeView setGoToNextProgressPolling:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];
        
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            //开始巡检
            PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
            detailVc.warningUuid=model.H_uuid;
            detailVc.type=2;
            detailVc.state=model.H_state;
            [blockSelf pushController:detailVc];
        }
    }];
#pragma mark 播放语音首页
    [_homeView setPlaySound:^(HomeModel* model) {
        [blockSelf hiddenMoreTable];

        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
            
        }else
        {
        }
        
    }];
    _photosAr=[[NSMutableArray alloc]init];
#pragma mark 放大图片首页
    
    [_homeView setBlowUpImg:^(HomeModel* model,NSString* num) {
        [blockSelf hiddenMoreTable];

        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            NSString*urlString =[CClass getCacheData:qnDomain];
            [blockSelf.photosAr removeAllObjects];
            NSString*imgStr=model.H_image;
            NSString *videoStr=model.H_video;
            
            if ((imgStr ==nil||[imgStr isEqualToString:@""])) {
                if (videoStr.length>0) {
#pragma mark 播放视频
                    
                    [blockSelf.photosAr addObject:[MWPhoto videoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,videoStr]]]];
                }
            }else
            {
                NSArray *array = [imgStr componentsSeparatedByString:@","];
                for (int i=0; i<array.count; i++) {
                    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,[array objectAtIndex:i]]];
                    [blockSelf.photosAr addObject:[MWPhoto photoWithURL:url]];
                    
                }
                if (videoStr.length>0) {
#pragma mark 播放视频
                    
                    [blockSelf.photosAr addObject:[MWPhoto videoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,videoStr]]]];
                }
            }
            
            
            _homebrowser = [[MWPhotoBrowser alloc] initWithDelegate:blockSelf];
            // Present
            [blockSelf.navigationController pushViewController:blockSelf.homebrowser animated:YES];
            blockSelf.homebrowser.displayActionButton = NO;
            blockSelf.homebrowser.displayNavArrows = NO;
            blockSelf.homebrowser.displaySelectionButtons = NO;
            blockSelf.homebrowser.zoomPhotosToFill = YES;
            blockSelf.homebrowser.alwaysShowControls = NO;
            blockSelf.homebrowser.enableGrid = YES;
            blockSelf.homebrowser.startOnGrid = NO;
            blockSelf.homebrowser.autoPlayOnAppear = NO;
            
            [blockSelf.homebrowser setCurrentPhotoIndex:[num integerValue]];
            
            UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
            [backView bk_whenTapped:^{
                [blockSelf.navigationController popViewControllerAnimated:YES];
            }];
            
            UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_backWhite"];
            UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, (40-icoBack.size.height)/2, icoBack.size.width, icoBack.size.height)];
            imgBack.image=icoBack;
            imgBack.userInteractionEnabled=YES;
            [backView addSubview:imgBack];
            
            UILabel *backTitle = [[UILabel alloc] init];
            backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 0, 50 - imgBack.frame.size.width,40);
            
            [backTitle setText:@"返回"];
            [backTitle setTextColor:[UIColor whiteColor]];
            [backTitle setFont:F(14)];
            [backView addSubview:backTitle];
            UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
            blockSelf.homebrowser.navigationItem.leftBarButtonItem = leftBarButtonItem;
            
        }
        
        
    }];
#pragma mark 进入完成点击事件
    [_homeView setGotoComplete:^(NSMutableDictionary *dic) {
        CompleteViewController*completeView=[[CompleteViewController alloc]init];
        completeView.dataDic=dic;
        [blockSelf pushController:completeView];
    }];
#pragma mark 进入设备补充
    [_homeView setCCellGotoAdd:^(HomeModel* model) {
        
        [blockSelf hiddenMoreTable];

     if ((model.H_machine_name ==nil||[model.H_machine_name isEqualToString:@""])&&(model.H_machine_type_name ==nil||[model.H_machine_type_name isEqualToString:@""])) {


//         
         EquipmentViewController *equipVc=[[EquipmentViewController alloc]init];
         equipVc.orderUuid=model.H_uuid;;
         equipVc.instUuid=model.H_inst_uuid;
         equipVc.num=[model.H_state integerValue];
         //有单
         [blockSelf pushController:equipVc];

         
     }else if ((model.H_machine_name ==nil||[model.H_machine_name isEqualToString:@""])||(model.H_machine_type_name ==nil||[model.H_machine_type_name isEqualToString:@""])) {
         
         if(model.H_machine_name ==nil||[model.H_machine_name isEqualToString:@""]){
             HistoryViewController *historyVC =[[HistoryViewController alloc]init];
             historyVC.machineUuid =model.H_machine_uuid;
             historyVC.num=[model.H_state integerValue];
             historyVC.orderUuid=model.H_uuid;
             historyVC.Type=MachineLink;//不需要关联需要补充
             [weakview pushController:historyVC];
         }else{
           
             HistoryViewController *historyVC =[[HistoryViewController alloc]init];
             historyVC.machineUuid =model.H_machine_uuid;
             historyVC.num=[model.H_state integerValue];
             historyVC.Type=MachineLink;//不需要关联需要补充
             historyVC.orderUuid=model.H_uuid;
             [weakview pushController:historyVC];
         }
        
     }
        
    }];
   
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    [narView bk_whenTapped:^{
        [self hiddenMoreTable];
    }];
    narView.backgroundColor=MainColor;
    [_passscrollview addSubview:narView];

    UIImage *historyImg =[UIImage imageNamed:@"Ico_home_new"];

    UIView *history = [[UIView alloc]initWithFrame:CGRectMake(0, 64-historyImg.size.height-10-5, historyImg.size.width+22, historyImg.size.height+5+15)];
    [narView addSubview:history];
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(17, 5, historyImg.size.width, historyImg.size.height)];
    [history addSubview:imageView];
    imageView.userInteractionEnabled =YES;
    imageView.image =historyImg;
     _messageLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)-2.5,2.5, 5, 5)];
    _messageLab.layer.masksToBounds=YES;
    _messageLab.layer.cornerRadius=2.5;
    _messageLab.backgroundColor=[UIColor clearColor];
    _messageLab.hidden=YES;
    _messageLab.userInteractionEnabled=YES;
    _messageLab.textAlignment=NSTextAlignmentCenter;
    _messageLab.textColor=[UIColor whiteColor];
    [history addSubview:_messageLab];
    [self.homeView setGetMessageStr:^(NSString *messageStr) {
        blockSelf.messageLab.backgroundColor=[UIColor clearColor];

        if ([messageStr isEqualToString:@"0"]||messageStr==nil) {
            blockSelf.messageLab.hidden=YES;
            blockSelf.messageLab.backgroundColor=[UIColor clearColor];

        }else
        {
            blockSelf.messageLab.hidden=NO;
            blockSelf.messageLab.backgroundColor=C(@"f9e855");

        }
 
    }];

#pragma mark 进入消息
    [history bk_whenTapped:^{
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"stopPlayer" object:nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"stopHistoryPlayer" object:nil];
        [blockSelf hiddenMoreTable];

        MessageViewController *messageVC =[[MessageViewController alloc]init];
        [self pushController:messageVC];
        }
     
    }];
    
    
    UIImage *moreImg = [UIImage imageNamed:@"Ico_home_more"];
    //75+moreImg.size.width
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(50, 20,WindowWidth-100, 44)];
    [narView addSubview:titleView];

    
    UILabel *lab =[[UILabel alloc]initWithFrame:CGRectMake((titleView.bounds.size.width-18*4-moreImg.size.width)/2, 10, 20*4, 26)];
    [titleView addSubview:lab];

    lab.userInteractionEnabled=YES;
    lab.text =@"邮易维";
    lab.textAlignment=NSTextAlignmentCenter;
    lab.textColor =[UIColor whiteColor];
    lab.font = F(16);
    _headView =[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab.frame),10+(26-moreImg.size.height)/2, moreImg.size.width, moreImg.size.height)];
    [titleView addSubview:_headView];

    _headView.image =moreImg;
    _headView.userInteractionEnabled=YES;
    _moreNum=0;
    [titleView bk_whenTapped:^{
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"stopPlayer" object:nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"stopHistoryPlayer" object:nil];
        blockSelf.addView.hidden=YES;
        _moreNum++;
        if (_moreNum%2==0) {
            _headView.image =[UIImage imageNamed:@"Ico_home_more"];
            _homeTable.hidden=YES;
            
            
        }else
        {
            _headView.image =[UIImage imageNamed:@"Ico_home_small"];
            _homeTable.hidden=NO;
            
        }
        }
    }];
    //选择列表
    _homeTable=[[HomeMoreTable alloc]initWithFrame:CGRectMake((WindowWidth-100)/2, 64, 100, 40*7)];
    _homeTable.hidden=YES;
    _homeTable.clipsToBounds=NO;
    _homeTable.layer.shadowColor = [UIColor blackColor].CGColor;//阴影颜色
    _homeTable.layer.shadowOffset = CGSizeMake(0, 0);//偏移距离
    _homeTable.layer.shadowOpacity = 0.5;//不透明度
    _homeTable.layer.shadowRadius = 5.0;//半径
    _homeTable.tag=10002;
    [[[UIApplication sharedApplication] keyWindow] addSubview:_homeTable];


#pragma mark 不同的状态
    [_homeTable setCCellSelect:^(NSMutableDictionary *dic) {
        blockSelf.homeTable.hidden=YES;
        blockSelf.headView.image =[UIImage imageNamed:@"Ico_home_more"];
        if([[dic objectForKey:@"type"] isEqualToString:@"当前任务"])
        {
            lab.text=@"邮易维";
        }else
        {
            lab.text=[dic objectForKey:@"type"];

        }
//        blockself.messageLab.text=blockself.homeView.messageStr;
        int num =[[dic objectForKey:@"num"] integerValue];
        //01：待接单 02：待处理 04：处理中 06：挂起中
        if (num==0) {
            //当前任务
            blockSelf.homeView.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(-500, 64, 0, WindowHeight-64-49);
            [blockSelf.homeView setWithType:@""];
            
        }else if (num==1) {
            //待接单
            blockSelf.homeView.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(-500, 64, 0, WindowHeight-64-49);
            [blockSelf.homeView setWithType:@"01"];

            
        }else if (num==2) {
            //待处理
            blockSelf.homeView.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(-500, 64, 0, WindowHeight-64-49);
            [blockSelf.homeView setWithType:@"02"];

            
        }else if (num==3) {
            //处理中
            blockSelf.homeView.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(-500, 64, 0, WindowHeight-64-49);
            [blockSelf.homeView setWithType:@"04"];

            
        }else if (num==4) {
            //挂起中
            blockSelf.homeView.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(-500, 64, 0, WindowHeight-64-49);
            [blockSelf.homeView setWithType:@"06"];
            
        }else if(num==5)
        {
            //待确认
            blockSelf.homeView.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(-500, 64, 0, WindowHeight-64-49);
            [blockSelf.homeView setWithType:@"20"];


        }else if(num==6)
        {
            //历史任务
            blockSelf.homeView.frame=CGRectMake(-500, 64, WindowWidth, WindowHeight-64-49);
            blockSelf.errorHistory.frame=CGRectMake(0, 64, WindowWidth, WindowHeight-64-49);
            [blockSelf.view bringSubviewToFront:blockSelf.addView];

        }
        [blockSelf.view bringSubviewToFront:blockSelf.homeTable];

        
    }];
    
    UIImage *addImg =[UIImage imageNamed:@"Ico_home_narAdd"];
    UIView *addView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-16-addImg.size.width, 30, addImg.size.width+16, addImg.size.height)];
    [narView addSubview:addView];
    UIImageView *addImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, addImg.size.width, addImg.size.height)];
    addImageView.image=addImg;
    [addView addSubview:addImageView];
    addImageView.userInteractionEnabled=YES;
    UIImage *addBg=[UIImage imageNamed:@"Ico_home_addBG"];//@"Ico_home_scan",@"扫一扫",
    NSArray*nameAr=[NSArray arrayWithObjects:@"扫二维码",@"录入报修",@"我的录入",@"发起巡检", nil];
    CGFloat imgWidth=addBg.size.width-20;
    
    _addView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-imgWidth-15, 64+3,imgWidth, addBg.size.height+40*nameAr.count)];
    _addView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_addView];
    [self.view bringSubviewToFront:_addView];
    
    UIImageView*addImgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imgWidth, addBg.size.height)];
    addImgView.image=addBg;
    [_addView addSubview:addImgView];
    UIView *imgBgView=[[UIView alloc]initWithFrame:CGRectMake(0, addBg.size.height,imgWidth, 40*nameAr.count)];
    imgBgView.backgroundColor=MainColor;
    imgBgView.layer.cornerRadius=5;
    imgBgView.layer.masksToBounds=YES;
    [_addView addSubview:imgBgView];
    _addView.hidden=YES;
    for (int i=0; i<nameAr.count; i++) {
        UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 40*i, imgWidth, 40)];
        bgView.tag=1000+i;
        [imgBgView addSubview:bgView];
        
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,imgWidth, 40)];
        lab.text=[NSString stringWithFormat:@"%@",[nameAr objectAtIndex:i]];
        [bgView addSubview:lab];
        lab.userInteractionEnabled=YES;
        lab.textColor=[UIColor whiteColor];
        lab.font=FF(15);
        lab.textAlignment=NSTextAlignmentCenter;
        UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(15, 40-0.5, imgBgView.bounds.size.width-15-7, 0.5)];
        line.backgroundColor=C(@"fd7d60");
        [bgView  addSubview:line];
        if (i==0){
#pragma mark 扫二维码
            
            [bgView bk_whenTapped:^{
                _addView.hidden=YES;
                
                
                //扫码
                ScanViewController *scanVC =[[ScanViewController alloc]init];
                [scanVC setDataType:NoListdata];
                scanVC.num=1000;
                scanVC.orderUuid=@"";
                scanVC.instUuid=@"";
                [self pushController:scanVC];
                
                
                
            }];
        }else if (i==1){
#pragma mark 录入报修
            
            [bgView bk_whenTapped:^{
                _addView.hidden=YES;
                
                
                InputViewController *inputVC=[[InputViewController alloc]init];
                [self pushController:inputVC];
                
                
                
            }];
        }else if (i==2){
#pragma mark 我的录入
            
            [bgView bk_whenTapped:^{
                _addView.hidden=YES;
                
                
                MyInputViewController *myinputVC=[[MyInputViewController alloc]init];
                [self pushController:myinputVC];
                
                
            }];
            
        }else if (i==3)
        {
            [bgView bk_whenTapped:^{
                _addView.hidden=YES;
#pragma mark 发起巡检
                InitiatePollingViewController *initiateVC=[[InitiatePollingViewController alloc]init];
                [self pushController:initiateVC];
                
                
                
            }];
            
            
        }
    }
    
    [addView bk_whenTapped:^{
        if (self.isNetwork==NO) {
            [CClass showLittleTimeMessage:@"没有网络！" completion:nil];
            
        }else
        {
            
        [[NSNotificationCenter defaultCenter]postNotificationName:@"stopPlayer" object:nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"stopHistoryPlayer" object:nil];
      [blockSelf hiddenMoreTable];
        _addNum++;
        if (_addNum%2==0) {
            _addView.hidden=YES;
            
            
        }else
        {
            _addView.hidden=NO;
            
        }
        }
    }];
    
    [_homeView setScrollView:^(CGFloat ofY) {
        [blockSelf hiddenMoreTable];
        
    }];
    [_passscrollview addSubview:_homeView];
    
    [_errorHistory setScrollView:^(CGFloat ofY) {
        [blockSelf hiddenMoreTable];

    }];

    [self.view bringSubviewToFront:self.homeTable];

    

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if(_homeView.contentOffset.y<0) {
        
        
        
    }else if(_homeView.contentOffset.y<700){
        
        
    }
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    if (photoBrowser==_homebrowser) {
        return _photosAr.count;

    }else
    {
        return _historyPhotosAr.count;

    }
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (photoBrowser==_homebrowser) {
        if (index < _photosAr.count) {
            return [_photosAr objectAtIndex:index];
        }
        return nil;
    }else
    {
        if (index < _historyPhotosAr.count) {
            return [_historyPhotosAr objectAtIndex:index];
        }
        return nil;
    }
    
}
//自定义标题
- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser

      titleForPhotoAtIndex:(NSUInteger)index {
    return @"查看图片";
    
}
- (void)hiddenMoreTable
{
    _moreNum++;
    _headView.image =[UIImage imageNamed:@"Ico_home_more"];
    _homeTable.hidden=YES;
    _addView.hidden=YES;


}
- (UIView*)HeaderView{
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 400)];
    headView.backgroundColor=BackColor;

    
    UIImage *img =[UIImage imageNamed:@"Ico_register_noThrough"];
    UIImageView *throughImg =[[UIImageView alloc]initWithFrame:CGRectMake((WindowWidth-img.size.width)/2, 98, img.size.width, img.size.height)];
    [headView addSubview:throughImg];
    
    
    throughImg.image =img;
    UILabel *nameLab =[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(throughImg.frame)+10, WindowWidth, 20)];
    [headView addSubview:nameLab];
    
    nameLab.font=F(15);
    nameLab.textColor =MainColor;
    nameLab.textAlignment =NSTextAlignmentCenter;
    nameLab.text =@"尚未通过单位审核，请联系主管处理";
    return headView;
    
}

@end
